package gwtmodule1.client.gui.stock;

import gwtmodule1.client.gui.stock.PageEditItemList.ItemListParams;
import gwtmodule1.client.gui.utils.DekoTable;
import gwtmodule1.client.gui.utils.FrameClient;
import gwtmodule1.client.gui.utils.FrameProvider;
import gwtmodule1.client.gui.utils.SwitchPage;
import gwtmodule1.client.gui.utils.Title;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PageSubcategoryList extends VerticalPanel implements FrameClient {

	public static class SubcategoryParams {
		private int id;

		private String name;

		public SubcategoryParams(int id, String name) {
			super();
			this.id = id;
			this.name = name;
		}
	}

	private class SubcategoryTable extends DekoTable {
		public SubcategoryTable() {
			Button add = new Button("add");
			add.addClickListener(new SwitchPage(provider, pageEditSubcategory,
					new Integer(0)));
			addTitle("Subcategory");
			addTitle("", 3);
			addTitleWidget(add);
		}

		public void addItem(int id, String name) {
			Hyperlink items = new Hyperlink("edit items", "EditItems" + id);
			ItemListParams itemParam = new ItemListParams(id, name);
			items.addClickListener(new SwitchPage(provider, pageEditItemList,
					itemParam));

			Hyperlink prices = new Hyperlink("edit prices", "EditPrices" + id);
			prices.addClickListener(new SwitchPage(provider, pageEditPrices));

			Hyperlink subcat = new Hyperlink("edit Subcategory",
					"EditSubcategory" + id);
			subcat.addClickListener(new SwitchPage(provider,
					pageEditSubcategory, new Integer(id)));

			AddRow r = addRow();
			r.add(name);
			r.add(items);
			r.add(prices);
			r.add(subcat);
			r.add(new Button("delete"));
		}
	}

	private Title title;

	private FrameProvider provider;

	private PageEditItemList pageEditItemList;

	private PageEditSubcategory pageEditSubcategory;

	private PageEditPrices pageEditPrices;

	public PageSubcategoryList(FrameProvider provider) {
		super();
		this.provider = provider;
		pageEditItemList = new PageEditItemList(provider);
		pageEditPrices = new PageEditPrices();
		pageEditSubcategory = new PageEditSubcategory();
		add(title = new Title("Subcategories"));
		add(createTable());
	}

	private Widget createTable() {
		SubcategoryTable table = new SubcategoryTable();
		table.addItem(1, "TV");
		table.addItem(2, "DVD Player");
		table.addItem(3, "Gummibears");
		table.addItem(4, "Medicine");
		return table;
	}

	public void onShow(Object param) {
		SubcategoryParams p = (SubcategoryParams) param;
		title.setTitle("Subcategories of Category " + p.name);
	}

}
